package com.photoeditor.demo.ui.widget.image.emoji;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;

import com.photoeditor.demo.model.bean.EmojiBean;

import java.util.ArrayList;

/**
 * Emoji 面板的Adapter
 */
public class EmojiArrayAdapter extends ArrayAdapter<EmojiBean> {

    private Context mContext;

    private ArrayList<EmojiBean> mData;

    private int mItemWidth;

    private int mItemHeight;

    private int mEmojiWidth;

    private int mEmojiHeight;

    /**
     * @param context
     * @param data
     * @param itemWidth
     * @param itemHeight
     * @param emojiWidth
     * @param emojiHeight
     */
    public EmojiArrayAdapter(Context context, ArrayList<EmojiBean> data, int itemWidth, int itemHeight, int emojiWidth, int emojiHeight) {
        super(context, 0, data);
        this.mContext = context;
        this.mData = data;
        mItemWidth = itemWidth;
        mItemHeight = itemHeight;
        mEmojiWidth = emojiWidth;
        mEmojiHeight = emojiHeight;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public EmojiBean getItem(int position) {
        if (position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public boolean isEmpty() {
        if (mData == null || mData.size() == 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return super.isEnabled(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new EmojiItem(mContext, mEmojiWidth, mEmojiHeight);
            GridView.LayoutParams lp = new GridView.LayoutParams(mItemWidth, mItemHeight);
            convertView.setLayoutParams(lp);
        }
        EmojiItem item = ((EmojiItem) convertView);
        EmojiBean bean = getItem(position);
        item.setTag(bean);
        item.setItemData(bean);
        return convertView;
    }
}
